<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-search-group">
                        <div class="cms-left-btn">
                            <el-button type="primary" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                            <el-button type="primary" icon="fa fa-plus" @click="create" size="small"> 新增</el-button>
                        </div>
                    </div>
                    <!--<div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input  size="medium" @keydown.enter.native="list(1)" v-model="params.kwd" placeholder="请输入账号、名称或者标识" class="input-with-select">
                                <el-button type="primary" @click="list(1)" slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                    </div>-->
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              tooltip-effect="dark" :data="listData" border>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column prop="name" label="标识" width="180"></el-table-column>
                        <el-table-column prop="title" label="名称" width="180"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" plain @click="edit(scope.row)" icon="fa fa-edit">
                                    编辑
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </section>
        </el-main>
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" ref="form" label-width="80px">
                <el-form-item :rules="[{ required: true, message: '按钮标识', trigger: 'blur'}]" label="标识"  prop="name">
                    <el-input style="width: 100%;" v-bind:content="form.name"
                              v-model="form.name" placeholder="请填写按钮标识"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '按钮名称', trigger: 'blur'}]" label="名称"  prop="title">
                    <el-input style="width: 100%;" v-bind:content="form.title"
                              v-model="form.title" placeholder="请填写按钮名称"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update('form')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>
<!---->
<script>
    import {mapGetters} from 'vuex';
    import {AppletCountBtnList, AppletCountBtnUpdate} from '@/utils/request';

    export default {
        data() {
            return {
                params: {

                },
                listData: [],
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '550px'
                },
                form:null,
            }
        },
        activated() {
            this.$store.dispatch('setAppletCountActive', '/appletcount/btn-manage');
            this.list();
        },
        computed: {
            ...mapGetters(['active', 'loading']),
        },
        methods: {
            //列表
            list(page) {
                if (page) {
                    this.params.page = page;
                }
                this.$store.dispatch('setLoading', true);
                AppletCountBtnList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },

            //新增
            create(){
                this.form = {
                    id:0,
                    name:'',
                    title: '',
                };
                this.dialog.title = '新增按钮';
                this.dialog.show = true;
            },
            //编辑
            edit(info){
                this.form = {
                    id: info.id,
                    name: info.name,
                    title: info.title,
                };
                this.dialog.title = '编辑按钮';
                this.dialog.show = true;
            },
            update(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        AppletCountBtnUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .cms-left-btn {
        float: left;
    }

    .el-tab-pane {
        display: none;
    }
</style>
